package cn.iocoder.yudao.module.currency.controller.admin.rate;

import cn.iocoder.yudao.module.currency.service.rate.ExchangeRateQueryService;
import cn.iocoder.yudao.module.currency.service.rate.ExchangeRateSyncService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.currency.controller.admin.rate.vo.*;
import cn.iocoder.yudao.module.currency.dal.dataobject.rate.ExchangeRateDO;
import cn.iocoder.yudao.module.currency.service.rate.ExchangeRateService;

@Tag(name = "管理后台 - 货币汇率")
@RestController
@RequestMapping("/currency/exchange-rate")
@Validated
public class ExchangeRateController {

    @Resource
    private ExchangeRateService exchangeRateService;

    @Resource
    private ExchangeRateQueryService exchangeRateQueryService;

    @Resource
    private ExchangeRateSyncService exchangeRateSyncService;

    @PostMapping("/create")
    @Operation(summary = "创建货币汇率")
    @PreAuthorize("@ss.hasPermission('currency:exchange-rate:create')")
    public CommonResult<Long> createExchangeRate(@Valid @RequestBody ExchangeRateSaveReqVO createReqVO) {
        return success(exchangeRateService.createExchangeRate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新货币汇率")
    @PreAuthorize("@ss.hasPermission('currency:exchange-rate:update')")
    public CommonResult<Boolean> updateExchangeRate(@Valid @RequestBody ExchangeRateSaveReqVO updateReqVO) {
        exchangeRateService.updateExchangeRate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除货币汇率")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('currency:exchange-rate:delete')")
    public CommonResult<Boolean> deleteExchangeRate(@RequestParam("id") Long id) {
        exchangeRateService.deleteExchangeRate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得货币汇率")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('currency:exchange-rate:query')")
    public CommonResult<ExchangeRateRespVO> getExchangeRate(@RequestParam("id") Long id) {
        ExchangeRateDO exchangeRate = exchangeRateService.getExchangeRate(id);
        return success(BeanUtils.toBean(exchangeRate, ExchangeRateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得货币汇率分页")
    @PreAuthorize("@ss.hasPermission('currency:exchange-rate:query')")
    public CommonResult<PageResult<ExchangeRateRespVO>> getExchangeRatePage(@Valid ExchangeRatePageReqVO pageReqVO) {
        PageResult<ExchangeRateDO> pageResult = exchangeRateService.getExchangeRatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExchangeRateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出货币汇率 Excel")
    @PreAuthorize("@ss.hasPermission('currency:exchange-rate:export')")
    @OperateLog(type = EXPORT)
    public void exportExchangeRateExcel(@Valid ExchangeRatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExchangeRateDO> list = exchangeRateService.getExchangeRatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "货币汇率.xls", "数据", ExchangeRateRespVO.class,
                        BeanUtils.toBean(list, ExchangeRateRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得货币汇率列表")
    @PreAuthorize("@ss.hasPermission('currency:exchange-rate:query')")
    public CommonResult<List<ExchangeRateRespVO>> getExchangeRatePage(@Valid ExchangeRateListReqVO listReqVO) {
        List<ExchangeRateDO> pageResult = exchangeRateQueryService.getAvailableCurrencyList(listReqVO.getCurrencyName(),
            listReqVO.getCurrencyCode(), listReqVO.getAreaName());
        return success(BeanUtils.toBean(pageResult, ExchangeRateRespVO.class));
    }

    @GetMapping("/code")
    @Operation(summary = "根据编码获得货币汇率")
    @Parameter(name = "code", description = "编码", required = true, example = "CNY")
    @PreAuthorize("@ss.hasPermission('currency:exchange-rate:query')")
    public CommonResult<ExchangeRateRespVO> getExchangeRateByCode(@RequestParam("code") String code) {
        ExchangeRateDO exchangeRate = exchangeRateQueryService.getExchangeRate(code);
        return success(BeanUtils.toBean(exchangeRate, ExchangeRateRespVO.class));
    }

    @GetMapping("/sync-test")
    @Operation(summary = "测试汇率同步")
    @PreAuthorize("@ss.hasPermission('currency:exchange-rate:query')")
    public CommonResult<Integer> getExchangeRateByCode() {
        int synced = exchangeRateSyncService.syncAllRate();
        return success(synced);
    }

}
